package com.core.zyyz.amc.modules.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.utils.uuid.UUID;
import com.core.zyyz.amc.modules.domain.AmcProjectNode;
import com.core.zyyz.amc.modules.mapper.AmcBusinessCategoriesMapper;
import com.core.zyyz.amc.modules.mapper.AmcProjectSubnodeMapper;
import com.core.zyyz.amc.modules.service.AmcBusinessCategoriesService;
import com.core.zyyz.amc.modules.service.AmcProjectNodeService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcProjectSubnode;
import com.core.zyyz.amc.modules.service.AmcProjectSubnodeService;
import com.core.common.core.page.TableDataInfo;

/**
 * 子节点配置Controller
 *
 * @author LiuFushun
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/amc/subnode")
public class AmcProjectSubnodeController extends BaseController
{
    @Autowired
    private AmcProjectSubnodeService amcProjectSubnodeService;
    @Autowired
    private AmcProjectSubnodeMapper amcProjectSubnodeMapper;
    @Autowired
    private AmcBusinessCategoriesMapper amcBusinessCategoriesMapper;
    @Autowired
    private AmcProjectNodeService amcProjectNodeService;
    @Autowired
    private AmcBusinessCategoriesService amcBusinessCategoriesService;

    /**
     * 查询子节点配置列表
     */
    @PreAuthorize("@ss.hasPermi('amc:subnode:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcProjectSubnode amcProjectSubnode)
    {
        startPage();
        List<AmcProjectSubnode> list = amcProjectSubnodeService.selectAmcProjectSubnodeList(amcProjectSubnode);
        return getDataTable(list);
    }

    /**
     * 获取子节点配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:subnode:query')")
    @GetMapping(value = "/{subnodeId}")
    public AjaxResult getInfo(@PathVariable("subnodeId") String subnodeId)
    {
        return success(amcProjectSubnodeService.selectAmcProjectSubnodeBySubnodeId(subnodeId));
    }

    /**
     * 新增子节点配置
     */
    @PreAuthorize("@ss.hasPermi('amc:subnode:add')")
    @Log(title = "子节点配置", businessType = BusinessType.INSERT)
    @PostMapping("/insertSubNode")
    @Transactional
    public AjaxResult add(@RequestBody AmcProjectSubnode amcProjectSubnode) {
        amcProjectSubnode.setSubnodeId(UUID.randomUUID().toString());
        int count = amcProjectSubnodeMapper.checkIsExist(amcProjectSubnode);
        if (count == 0){
            amcProjectSubnodeService.insertAmcProjectSubnode(amcProjectSubnode);
            amcBusinessCategoriesMapper.deleteCategoriesNode(amcProjectSubnode.getSubnodeId());
            return success();
        }else {
            return warn("子节点代码在配置中已存在!!!");
        }
    }

    /**
     * 修改子节点配置
     */
    @PreAuthorize("@ss.hasPermi('amc:subnode:edit')")
    @Log(title = "子节点配置", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSubNode")
    @Transactional
    public AjaxResult edit(@RequestBody AmcProjectSubnode amcProjectSubnode) {
        int count = amcProjectSubnodeMapper.checkIsExist(amcProjectSubnode);
        if (count == 0){
            amcProjectSubnodeService.updateAmcProjectSubnode(amcProjectSubnode);
            String subNodeId = amcProjectSubnode.getSubnodeId();
            amcBusinessCategoriesMapper.deleteCategoriesNode(subNodeId);
            String[] baseProjectType = amcProjectSubnode.getBaseProjectType();
            if (baseProjectType != null && baseProjectType.length != 0) {
                List<String> typeList = Arrays.asList(baseProjectType);
                String nodeCode = amcProjectSubnode.getNodeCode();
                AmcProjectNode amcProjectNode = amcProjectNodeService.selectAmcProjectNodeByNodeCode(nodeCode);
                if (amcProjectNode != null) {
                    String nodeId = amcProjectNode.getNodeId();
                    typeList.stream().forEach(i -> {
                        Map<String, Object> map = new HashMap<>();
                        map.put("nodeId", nodeId);
                        map.put("subnodeId", subNodeId);
                        map.put("bizId", i);
                        int count1 = amcBusinessCategoriesService.getMappingCount(map);
                        if (count1 == 0) {//启用
                            amcBusinessCategoriesService.addCategoriesNode(map);
                        }
                    });
                }
            }
            return success();
        }else {
            return warn("子节点代码在配置中已存在!!!");
        }
    }

    /**
     * 删除子节点配置
     */
    @PreAuthorize("@ss.hasPermi('amc:subnode:remove')")
    @Log(title = "子节点配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{subnodeIds}")
    public AjaxResult remove(@PathVariable String[] subnodeIds)
    {
        return toAjax(amcProjectSubnodeService.deleteAmcProjectSubnodeBySubnodeIds(subnodeIds));
    }

//    /**
//     * 根据主节点查询子节点
//     */
//    @PreAuthorize("@ss.hasPermi('amc:subnode:list')")
//    @GetMapping("/querySubNodeByNode")
//    public TableDataInfo querySubNodeByNode(String nodeCode) {
//        startPage();
//        List<AmcProjectSubnode> list = amcProjectSubnodeService.querySubNodeByNode(nodeCode);
//        return getDataTable(list);
//    }

//    /**
//     * 根据主节点查询子节点列表(树)
//     */
//    @GetMapping("/query/subNodeList")
//    public TableDataInfo querySubnodeByNodeTree(String nodeCode){
//        List<AmcProjectSubnode> list = amcProjectSubnodeService.querySubNodeByNode(nodeCode);
//        List<HashMap<String ,Object>> resultlist = new ArrayList<>();
//        list.stream().forEach(i->{
//            HashMap<String ,Object> subtonode = new HashMap<>();
//            subtonode.put("subnodeId",i.getSubnodeId());
//            subtonode.put("nodeName",i.getSubnodeName());
//            subtonode.put("isLeaf",true);
//            resultlist.add(subtonode);
//        });
//        return getDataTable(resultlist);
//    }
}
